<?php 
class Login extends CI_Controller {
	private $error = array();
	
	public function index() {
		if ($this->customer->isLogged()) {  
      		redirect(site_url().'/account/account');
    	}
	
    	$this->lang->load('account/login');

    	$this->document->setTitle($this->lang->line('heading_title'));
						
		if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validate()) {
			$this->session->unset_userdata('guest');
			
			$this->load->model('account/Model_Address');

			$address_info = $this->Model_Address->getAddress($this->customer->getAddressId());

			if ($address_info) {
				$this->tax->setZone($address_info['country_id'], $address_info['zone_id']);
			}			
			
			// Added strpos check to pass McAfee PCI compliance test (http://forum.opencart.com/viewtopic.php?f=10&t=12043&p=151494#p151295)
			if ($this->input->post('redirect') && (strpos($this->input->post('redirect'), base_url()) !== false/* || strpos($this->input->post('redirect'), HTTPS_SERVER) !== false*/)) {
				redirect(str_replace('&amp;', '&', $this->input->post('redirect')));
			} else {
				redirect(site_url('account/account')); 
			}
    	}  
		
      	$data['breadcrumbs'] = array();

      	$data['breadcrumbs'][] = array(
        	'text'      => $this->lang->line('text_home'),
			'href'      => site_url('common/home'),       	
        	'separator' => false
      	);
  
      	$data['breadcrumbs'][] = array(
        	'text'      => $this->lang->line('text_account'),
			'href'      => site_url('account/account'),
        	'separator' => $this->lang->line('text_separator')
      	);
		
      	$data['breadcrumbs'][] = array(
        	'text'      => $this->lang->line('text_login'),
			'href'      => site_url('account/login'),      	
        	'separator' => $this->lang->line('text_separator')
      	);
				
    	$data['heading_title'] = $this->lang->line('heading_title');

    	$data['text_new_customer'] = $this->lang->line('text_new_customer');
    	$data['text_register'] = $this->lang->line('text_register');
    	$data['text_right_register'] = $this->lang->line('text_right_register');//added by W.Q.CHING
    	$data['text_register_account'] = $this->lang->line('text_register_account');
		$data['text_returning_customer'] = $this->lang->line('text_returning_customer');
		$data['text_i_am_returning_customer'] = $this->lang->line('text_i_am_returning_customer');
    	$data['text_forgotten'] = $this->lang->line('text_forgotten');

    	$data['entry_email'] = $this->lang->line('entry_email');
    	$data['entry_password'] = $this->lang->line('entry_password');

    	$data['button_continue'] = $this->lang->line('button_continue');
		$data['button_login'] = $this->lang->line('button_login');

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}
		
		$data['action'] = site_url('account/login');
		$data['register'] = site_url('account/register');
		$data['forgotten'] = site_url('account/forgotten');

    	// Added strpos check to pass McAfee PCI compliance test (http://forum.opencart.com/viewtopic.php?f=10&t=12043&p=151494#p151295)
		if ($this->input->post('redirect') && (strpos($this->input->post('redirect'), HTTP_SERVER) !== false || strpos($this->input->post('redirect'), HTTPS_SERVER) !== false)) {
			$data['redirect'] = $this->input->post('redirect');
		} elseif ($this->session->userdata('redirect')) {
      		$data['redirect'] = $this->session->userdata('redirect');
	  		
			$this->session->unset_userdata('redirect');		  	
    	} else {
			$data['redirect'] = '';
		}

		if ($this->session->userdata('success')) {
    		$data['success'] = $this->session->userdata('success');
    
			$this->session->unset_userdata('success');
		} else {
			$data['success'] = '';
		}

		$data['header'] = helper_load_header();
		$data['column_left'] = helper_load_left();
		$data['column_right'] = helper_load_right();
		$data['content_top'] = helper_load_top();
		$data['content_bottom'] = helper_load_bottom();
		$data['footer'] = helper_load_footer();
		
		
		$this->load->view("account/login", $data);
		
		
		
  	}
  
  	private function validate() {
    	if (!$this->customer->login($this->input->post('email'), $this->input->post('password'))) {
      		$this->error['warning'] = $this->lang->line('error_login');
    	}
	
    	if (!$this->error) {
      		return true;
    	} else {
      		return false;
    	}  	
  	}
}
?>